package com.itheima.array2;

public class QuChongArray {
    /*
        需求：定义一个静态数组{1,2,3,4,5,6,7,8,2,1,4,5,2,3,4,5,8},去除重复数据输出{1,2,3,4,5,6,7,8}
     */
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,2,1,4,5,2,3,4,5,8};
        int count = 0;
        //将重复的元素值变更为0
        lo:for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1;j < arr.length; j++){
                if (arr[i] == arr[j]){
                    arr[i] = 0;
                    continue lo;
                }
            }
        }
        //使用计数器记录不重复元素的个数
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != 0){
                count++;
            }
        }

        //取得不重复的元素用arr2数组存储，元素个数为count记录的个数
        int[] arr2 = new int[count];
        int j = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != 0){
                arr2[j] = arr[i];
                j++;
            }
        }

        //冒泡排序将arr2数组中元素按从小到大顺序排列
        int temp;
        for (int i = 0; i < arr2.length; i++) {
            for (int x = 0; x < arr2.length - i - 1; x++) {
                if (arr2[x] > arr2[x + 1]) {
                    temp = arr2[x];
                    arr2[x] = arr2[x + 1];
                    arr2[x + 1] = temp;
                }
            }
        }

        //输出数组arr2中的元素
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }
}
